package cn.chenxiejia;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

/***
 * @program: maven
 * @ClassName: Imges6
 * @Description:
 * @Author: chenxiejia
 * @Date: 2022/1/23 1:17
 * @version : V1.0
 */
public class Imges6 {
    public static void main(String[] args) {
        var i = new BufferedImage(400, 400, 2);
        var m = i.createGraphics();
        m.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_GASP);
        m.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        m.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_DEFAULT);
        m.setColor(Color.YELLOW);
        m.fillRect(0, 0, 400, 400);


        String s = "陈卸甲";
        m.setColor(Color.RED);
        Font font = new Font("微软雅黑", Font.BOLD, 50);
        var is = Imges6.class.getClassLoader().getResourceAsStream("font/f1.ttf");
        try {
            font = Font.createFont(Font.TRUETYPE_FONT, is);
            System.out.println(font.getName());
        } catch (FontFormatException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        font.deriveFont(50f);

        var fm = m.getFontMetrics(font);
        m.setFont(font);


        m.drawString(s, 10, 50);
        m.dispose();
        try {
            ImageIO.write(i, "png", new File("ziti.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            Runtime.getRuntime().exec("cmd /k start ziti.png");
        } catch (IOException e) {
            e.printStackTrace();
        }


    }
}
